/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.fee.service;

import com.jeeplus.common.utils.PrimaryGenerater;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.fee.entity.Payable;
import com.jeeplus.modules.fee.mapper.PayableMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 应付结算单Service
 * @author weisong
 * @version 2017-09-18
 */
@Service
@Transactional(readOnly = true)
public class PayableService extends CrudService<PayableMapper, Payable> {

	public Payable get(String id) {
		return super.get(id);
	}
	
	public List<Payable> findList(Payable payable) {
		return super.findList(payable);
	}
	
	public Page<Payable> findPage(Page<Payable> page, Payable payable) {
		return super.findPage(page, payable);
	}
	
	@Transactional(readOnly = false)
	public void save(Payable payable) {
		super.save(payable);
	}
	
	@Transactional(readOnly = false)
	public void delete(Payable payable) {
		super.delete(payable);
	}

	// 更新开票金额
	@Transactional(readOnly = false)
	public int modifyInvoiceMoneyById(Double invoiceMoney, String id) {
		return mapper.modifyInvoiceMoneyById(invoiceMoney, id);
	}

	// 更新开票金额和状态
	@Transactional(readOnly = false)
	public int updateMoneyAndStatusById(Integer status, Double invoiceMoney, Double vcaMoney,Double noInvoiceMoney,Double noVcaMoney, String id) {
		return mapper.updateMoneyAndStatusById(status, invoiceMoney, vcaMoney,noInvoiceMoney, noVcaMoney, id);
	}

	// 查询最大的单号和日期【用来生成单号】
	public Payable findClearingNoBusyDate() {
		return mapper.findClearingNoBusyDate();
	}

	//根据付款单子表id查询对应的应付结算单
	public Payable findByPamentDetailId(String id){
		return mapper.findByPamentDetailId(id);
	}

	//付款单引入已确认的数据
	public List<Payable> findAllByStatus(String payee,Integer status){
		return mapper.findAllByStatus(payee,status);
	}

	//新增应付结算单生成单号
	public String createPayClearNo(){
		//生成单号
		String number = "";
		Payable payble = mapper.findClearingNoBusyDate();
		Format format1 = new SimpleDateFormat("yyyyMMdd");    //初始化时间格式
		//比较结算日期是否和现在的日期相等
		if (null == payble) {
			number = PrimaryGenerater.getInstance().generaterNextNumber("YFJS", null);
		} else {
			if (String.valueOf(format1.format(payble.getBusyDate())).equals(String.valueOf(format1.format(new Date())))) {
				number = PrimaryGenerater.getInstance().generaterNextNumber("YFJS", payble.getClearingNo());
			} else {
				number = PrimaryGenerater.getInstance().generaterNextNumber("YFJS", null);
			}
		}
		return number;
	}
}